package com.ruoyi.web.controller.personnel;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.personnel.domain.TrainingCredits;
import com.ruoyi.personnel.service.ITrainingCreditsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 培训学分记录Controller
 *
 * @author ruoyi
 * @date 2021-02-26
 */
@Api("培训学分记录")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/training/credits" )
public class TrainingCreditsController extends BaseController {

    private final ITrainingCreditsService iTrainingCreditsService;

    /**
     * 查询培训学分记录列表
     */
    @ApiOperation("查询培训学分记录列表")
    @PreAuthorize("@ss.hasPermi('training:credits:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrainingCredits trainingCredits) {
        startPage();
        List<TrainingCredits> list = iTrainingCreditsService.queryList(trainingCredits);
        return getDataTable(list);
    }

    /**
     * 导出培训学分记录列表
     */
    @ApiOperation("导出培训学分记录列表")
    @PreAuthorize("@ss.hasPermi('training:credits:export')" )
    @Log(title = "培训学分记录" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(TrainingCredits trainingCredits) {
        List<TrainingCredits> list = iTrainingCreditsService.queryList(trainingCredits);
        ExcelUtil<TrainingCredits> util = new ExcelUtil<TrainingCredits>(TrainingCredits.class);
        return util.exportExcel(list, "credits" );
    }

    /**
     * 获取培训学分记录详细信息
     */
    @ApiOperation("获取培训学分记录详细信息")
    @PreAuthorize("@ss.hasPermi('training:credits:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(iTrainingCreditsService.getById(id));
    }

    /**
     * 新增培训学分记录
     */
    @ApiOperation("新增培训学分记录")
    @PreAuthorize("@ss.hasPermi('training:credits:add')" )
    @Log(title = "培训学分记录" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrainingCredits trainingCredits) {
        trainingCredits.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iTrainingCreditsService.save(trainingCredits) ? 1 : 0);
    }

    /**
     * 修改培训学分记录
     */
    @ApiOperation("修改培训学分记录")
    @PreAuthorize("@ss.hasPermi('training:credits:edit')" )
    @Log(title = "培训学分记录" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainingCredits trainingCredits) {
        return toAjax(iTrainingCreditsService.updateById(trainingCredits) ? 1 : 0);
    }

    /**
     * 删除培训学分记录
     */
    @ApiOperation("删除培训学分记录")
    @PreAuthorize("@ss.hasPermi('training:credits:remove')" )
    @Log(title = "培训学分记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(iTrainingCreditsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

}
